# -*- coding: utf-8 -*-
from django.shortcuts import render,HttpResponse
from account.account_api import *
from account.models import *
from monitor.models import *
from account.account_api import *

import base64

# Create your views here.

@require_login
def index(request):
    if request.method == 'GET':
        Username = request.session.get('user_name')
        Userdata = User.objects.all().count()
        # CmdbData = cmdb.objects.all().count()

    return render(request,'index.html',locals())

@require_login
def server_lists(request):
    if request.method == 'GET':
        Serverlist = servers.objects.all()
        ServerStatus = server_status
        listencelerys = listencelery

    return render(request,'serversys/server_lists.html',locals())

@require_login
def server_add(request):
    if request.method == "GET":
        Businessdata = business.objects.all()
        Deptdata = Dept.objects.all()
        Idcdata = idclist.objects.all()
        ServerType = server_type
        Server_system = server_system

        return render(request,'serversys/server_add.html',locals())

    hostname = request.POST.get('servername')
    externalip = request.POST.get('externalip')
    if request.method == "POST":
        if is_common_user(request):
            return HttpResponse(u'普通用户没有权限访问')
    if servers.objects.filter(hostname=hostname):
        return HttpResponse(u'主机已存在')
    elif servers.objects.filter(externalip=externalip):
        return HttpResponse(u'外网IP已存在')
    else:
        pass_wd = request.POST.get('passwd')
        passwd = base64.encodestring(pass_wd)
        internalip = request.POST.get('internalip')
        Business_id = request.POST.get('Business')
        Business = business.objects.get(id=Business_id)
        passwd = request.POST.get('passwd')
        dept_id = request.POST.get('dept')
        deptname = Dept.objects.get(id=dept_id)
        cpu = request.POST.get('cpu')
        cpumhz = request.POST.get('cpumhz')
        mem = request.POST.get('mem')
        vip = request.POST.get('vip')
        disk = request.POST.get('disk')
        idc_id = request.POST.get('idc')
        idc=idclist.objects.get(id=idc_id)
        type = request.POST.get('type')
        system = request.POST.get('system')
        desc = request.POST.get('desc')
        p=servers(hostname=hostname,externalip=externalip,internalip=internalip,passwd=passwd,
                  businessname=Business,dept=deptname,cpu=cpu,cpumhz=cpumhz,mem=mem,disk=disk,type=type,
                  system=system,comment=desc,idc=idc,virtip=vip)
        p.save()
        return HttpResponse(u'主机添加成功')

def server_edit(request,id):

    ServerData = servers.objects.get(id=id)
    Deptdata = Dept.objects.all()
    Businessdata = business.objects.all()
    Server_system = server_system
    Idcdata = idclist.objects.all()
    ServerType = server_type
    # b = "alsdjflqjwlejrlqjwelrjlqwekjr"
    passwd = base64.b64encode(ServerData.passwd)
    Passwd = base64.b64decode(passwd)


    return render(request,'serversys/server_edit.html',locals())



def server_del(request,id):
    if request.method == 'GET':
        if is_common_user(request):
            return HttpResponse(u'普通用户没有权限')
        servers.objects.get(id=id).delete()
        return HttpResponse(u'主机删除成功')

@require_login
def idc_list(request):
    if request.method ==  'GET':
        Idclists = idclist.objects.all()
        Idclevel = idc_level

        idcdict = {}
        for v in Idclists:
            csd = servers.objects.filter(idc=v.id)
            idcdict[v.id]=csd
        return render(request,'idcmanage/idc_list.html',locals())

@require_login
def idc_add(request):
    if request.method == "GET":
        ZoneData = zone.objects.all()
        Idclevel = idclist.objects.all()
        Idclevel = idc_level

        return render(request,'idcmanage/idc_add.html',locals())

    if request.method == "POST":
        idcname = request.POST.get('idcname')
        getzone = request.POST.get('zone')
        getzone = zone.objects.get(id=getzone)
        level = request.POST.get('level')
        desc = request.POST.get('desc')
        addr = request.POST.get('addr')

        p = idclist(idcname=idcname,idczone=getzone,idclevel=level,idcdesc=desc,idcaddr=addr)
        p.save()
        return HttpResponse(u'IDC添加成功')

@require_login
def idc_edit(request,idc_id):
    if request.method == "GET":
        idcdata = idclist.objects.get(id=idc_id)
        ZoneData = zone.objects.all()
        Idclevels = idc_level

        return render(request,'idcmanage/idc_edit.html',locals())

    if request.method == 'POST':
        if is_common_user(request):
            return HttpResponse(u'普通用户没有权限修改')

        idc_name = request.POST.get('idcname')
        if idclist.objects.filter(idcname=idc_name,id=idc_id):
            idcdata = idclist.objects.get(id=idc_id)
            idcdata.idcname = idc_name
            idcdata.idcdesc = request.POST.get('desc')
            zoned = request.POST.get('zone')
            idcdata.idczone = zone.objects.get(id=zoned)
            idcdata.idclevel = request.POST.get('level')
            idcdata.idcaddr = request.POST.get('addr')
            idcdata.save()
            return HttpResponse(u'IDC修改成功')

        if idclist.objects.filter(idcname=idc_name):
            return HttpResponse(u'IDC已存在,请重新输入')
        else:
            idcdata = idclist.objects.get(id=idc_id)
            idcdata.idcname = idc_name
            idcdata.idcdesc = request.POST.get('desc')
            zoned = request.POST.get('zone')
            idcdata.idczone = zone.objects.get(id=zoned)
            idcdata.idclevel = request.POST.get('level')
            idcdata.idcaddr = request.POST.get('addr')
            idcdata.save()
            return HttpResponse(u'IDC修改成功')

@require_login
def idc_del(request,idc_id):
    idcdata = idclist.objects.get(id=idc_id)

    if is_common_user(request):
        return HttpResponse(u'普通用户没有权限删除')
    if idcdata.servers_set.all():
        return HttpResponse(u'IDC不能删除，请先删除关联服务器')
    else:
        idclist.objects.filter(id=idc_id).delete()
        return HttpResponse(u'该IDC删除成功')

@require_login
def zone_list(request):
    if request.method == 'GET':
        ZoneData = zone.objects.all()

    return render(request,'zonemanage/zone_list.html',locals())

@require_login
def zone_add(request):
    if request.method == 'GET':

        return render(request,'zonemanage/zone_add.html')

    if request.method == 'POST':
        zone_name = request.POST.get('zone_name')

        if is_common_user(request):
            return HttpResponse(u'普通用户没有权限')

        if zone.objects.filter(name=zone_name):
            Warrmess = u'区域名已经存在'
            return HttpResponse(Warrmess)
        else:
            p=zone(name=zone_name)
            p.save()
            return HttpResponse(u'部门添加成功')

def zone_del(request,zone_id):
    idczone = zone.objects.get(id=zone_id)

    if is_common_user(request):
        return HttpResponse(u'普通用户没有权限')
    if zone_id == '1':
        return HttpResponse(u'默认地区不能删除')
    if idczone.idclist_set.all():
        return HttpResponse(u'区域有关联，请先删除关联IDC')
    else:
        zone.objects.get(id=zone_id).delete()
    return HttpResponse(u'区域删除成功')

def zone_edit(request,zone_id):
    if request.method == 'GET':
        zonedata = zone.objects.get(id=zone_id)

        return render(request,'zonemanage/zone_edit.html',locals())

    if request.method == 'POST':
        if is_common_user(request):
            return HttpResponse(u'普通用户没有权限修改')

        Zone_name = request.POST.get('zone_name')

        if zone.objects.filter(id=zone_id,name=Zone_name):
            zonedata = zone.objects.get(id=zone_id)
            zonedata.name = Zone_name
            zonedata.save()
            return HttpResponse(u'地区修改成功')

        if zone.objects.filter(name=Zone_name):
            return HttpResponse(u'该区域已经存在，请重新输入')
        else:
            zonedata = zone.objects.get(id=zone_id)
            zonedata.name = Zone_name
            zonedata.save()
            return HttpResponse(u'区域修改成功')

def Business_list(request):
    if request.method == 'GET':
        BusinessData = business.objects.all()
    return render(request,'Business/Business_list.html',locals())

def Business_add(request):
    if request.method == "GET":
        return render(request,'Business/Business_add.html')

    if request.method == "POST":
        if is_common_user(request):
            return HttpResponse(u'普通用户没有权限增加')

        name = request.POST.get('name')

        if business.objects.filter(name=name):
            Warrmess = u'业务已存在'
            return HttpResponse(Warrmess)
        else:
            name = request.POST.get('name')
            contactperson = request.POST.get('contactperson')
            contactphone = request.POST.get('contactphone')
            contactemail = request.POST.get('contactemail')
            p = business(name=name,contactperson=contactperson,contactphone=contactphone,contactemail=contactemail)
            p.save()
            return HttpResponse(u'业务添加成功')

def Business_del(request,bu_id):
    if is_common_user(request):
        return HttpResponse(u'普通用户没有权限访问')

    if bu_id == '1':
        return HttpResponse(u'默认业务不能删除')

    Businessdata = business.objects.get(id=bu_id)
    if Businessdata.servers_set.all():
        return HttpResponse(u'业务有关联，请先删除关联主机')
    else:
        business.objects.get(id=bu_id).delete()
        return HttpResponse(u'业务删除成功')

def Business_edit(request,bu_id):
    if request.method == 'GET':
        Businessdata = business.objects.get(id=bu_id)

        return render(request,'Business/Business_edit.html',locals())

    if request.method == 'POST':
        if is_common_user(request):
            return HttpResponse(u'普通用户没有权限修改')

        Businessname = request.POST.get('name')
        if business.objects.filter(id=bu_id,name=Businessname):
            busidata = business.objects.get(id=bu_id)
            busidata.name = Businessname
            busidata.contactperson = request.POST.get('contactperson')
            busidata.contactphone = request.POST.get('contactphone')
            busidata.contactemail = request.POST.get('contactemail')
            busidata.save()
            return HttpResponse(u'业务修改成功')





